package com.hbnu.tangyalu.jdbc;

import java.sql.*;

public class JdbcDemo05 {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        String url="jdbc:mysql://localhost:3306/class2311?serverTimezone=GMT&useSSL=false&characterEncoding=utf-8";
        String name="root";
        String pwd="123456";
        try {
            connection= DriverManager.getConnection(url,name,pwd);
//            String sql="insert into tb_user values(?,?,?,?)";
//            preparedStatement=connection.prepareStatement(sql);
//            preparedStatement.setInt(1,4);
//            preparedStatement.setString(2,"wangwu");
//            preparedStatement.setString(3,"1234");
//            preparedStatement.setDouble(4,5555.5);
            String sql="update tb_user set salary=5555.1 where id=4 ";
            preparedStatement=connection.prepareStatement(sql);
            int rows=preparedStatement.executeUpdate();
            System.out.println(rows>=1? "插入成功":"插入失败");

        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
        finally {
//            关闭资源从小到大，一个一个的关闭
            try {
                if (resultSet!=null){
                    resultSet.close();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }

            try {
                if (preparedStatement!=null){
                    preparedStatement.close();
                }
            }
            catch (SQLException e){
                e.printStackTrace();
            }

            try {
                if (connection!=null){
                    connection.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
